﻿using System;
using System.Runtime.InteropServices;

namespace JiveMessenger.Utilities
{
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
    public static class ScreenDetection
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        private static extern IntPtr GetShellWindow();
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);

        private static IntPtr _desktopHandle;
        private static IntPtr _shellHandle;

        public static bool IsApplicationFullScreen()
        {
            _desktopHandle = GetDesktopWindow();
            _shellHandle = GetShellWindow();

            bool runningFullScreen = false;
            IntPtr hWnd = GetForegroundWindow();

            if (!hWnd.Equals(IntPtr.Zero))
            {
                if (!(hWnd.Equals(_desktopHandle) || hWnd.Equals(_shellHandle)))
                {
                    RECT appBounds;
                    GetWindowRect(hWnd, out appBounds);

                    System.Drawing.Rectangle screenBounds = System.Windows.Forms.Screen.FromHandle(hWnd).Bounds;
                    if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height
                        && (appBounds.Right - appBounds.Left) == screenBounds.Width)
                    {
                        runningFullScreen = true;
                    }
                }
            }
           
            return runningFullScreen;
        }
    }
}
